package com.bbzn.device.client.service.impl.cwting;

import com.alibaba.fastjson.JSON;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.ct.request.RequestCancelCommand;
import com.bbzn.device.client.service.emq.OrderSendBaseService;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.aep_device_command.*;
import org.springframework.stereotype.Service;

/**
 * @Author wulongbo
 * @Date 2021/9/11 11:09
 * @Version 1.0
 */
@Service
public class OrderSendBaseServiceImpl implements OrderSendBaseService {

//    private static AepDeviceCommandClient client;

//    private static AepDeviceCommandLwmProfileClient lwmClient;

//    public static AepDeviceCommandClient getClient() {
//        if (client == null) {
//            synchronized (AepDeviceCommandClient.class) {
//                if (client == null) {
//                    client = AepDeviceCommandClient.newClient()
//                            .appKey(YG.APP_KEY)
//                            .appSecret(YG.APP_SECRET)
//                            .build();
//                }
//            }
//        }
//        return client;
//    }

//    public static AepDeviceCommandLwmProfileClient getLwmClient() {
//        if (lwmClient == null) {
//            synchronized (AepDeviceCommandLwmProfileClient.class) {
//                if (lwmClient == null) {
//                    lwmClient = AepDeviceCommandLwmProfileClient.newClient()
//                            .appKey(YG.APP_KEY)
//                            .appSecret(YG.APP_SECRET)
//                            .build();
//                }
//            }
//        }
//        return lwmClient;
//    }


    @Override
    public CancelCommandResponse cancelCommand(String masterKey, RequestCancelCommand requestCancelCommand) throws Exception {
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestCancelCommand).getBytes();
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CancelCommandRequest request = new CancelCommandRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明
        CancelCommandResponse createDeviceResponse = client.CancelCommand(request);
        System.out.println(createDeviceResponse);
        client.shutdown();
        return createDeviceResponse;
    }

    @Override
    public QueryCommandResponse queryCommand(String masterKey, String commandId, Integer productId, String deviceId) throws Exception {
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        QueryCommandRequest request = new QueryCommandRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setParamCommandId(commandId);    // single value
        request.setParamProductId(productId);    // single value
        request.setParamDeviceId(deviceId);    // single value
        QueryCommandResponse queryCommandResponse = client.QueryCommand(request);
        System.out.println(queryCommandResponse);
        client.shutdown();
        return queryCommandResponse;
    }

    @Override
    public QueryCommandListResponse queryCommandList(String masterKey, Integer productId, String deviceId, String startTime, String endTime, int pageNow, int pageSize) throws Exception {
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        QueryCommandListRequest request = new QueryCommandListRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setParamProductId(productId);    // single value
        request.setParamDeviceId(deviceId);    // single value
        request.setParamStartTime(startTime);    // single value
        request.setParamEndTime(endTime);    // single value
        request.setParamPageNow(pageNow);    // single value
        request.setParamPageSize(pageSize);    // single value
        QueryCommandListResponse queryCommandListResponse = client.QueryCommandList(request);
        System.out.println(queryCommandListResponse);
        client.shutdown();
        return queryCommandListResponse;
    }

    //    @Override
//    public CreateCommandLwm2mProfileResponse commandLwm2mProfile(String masterKey, RequestCreateCommandM2M requestCreateCommandM2M) throws Exception {
//        // 1.bean对象转换成byte数组
//        byte[] body = JSON.toJSONString(requestCreateCommandM2M).getBytes();
//        AepDeviceCommandLwmProfileClient client = getLwmClient();
//        CreateCommandLwm2mProfileRequest request = new CreateCommandLwm2mProfileRequest();
//        // set your request params here
//        request.setParamMasterKey(masterKey);    // single value
//        request.setBody(body);    //具体格式见前面请求body说明
//        CreateCommandLwm2mProfileResponse commandLwm2mProfileResponse = client.CreateCommandLwm2mProfile(request);
//        System.out.println(commandLwm2mProfileResponse);
//        client.shutdown();
//        return commandLwm2mProfileResponse;
//    }
}
